package com.liu.student.mapper;

import com.liu.student.entity.Enrollment;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface EnrollmentMapper {
    @Select("SELECT * FROM enrollment")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "enrollmentDate", column = "enrollment_date"),
        @Result(property = "status", column = "status")
    })
    List<Enrollment> findAll();

    @Select("SELECT * FROM enrollment WHERE student_id = #{studentId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "enrollmentDate", column = "enrollment_date"),
        @Result(property = "status", column = "status")
    })
    List<Enrollment> findByStudentId(Long studentId);

    @Select("SELECT * FROM enrollment WHERE course_id = #{courseId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "enrollmentDate", column = "enrollment_date"),
        @Result(property = "status", column = "status")
    })
    List<Enrollment> findByCourseId(Long courseId);

    @Select("SELECT * FROM enrollment WHERE id = #{id}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "enrollmentDate", column = "enrollment_date"),
        @Result(property = "status", column = "status")
    })
    Enrollment findById(Long id);

    @Select("SELECT * FROM enrollment WHERE student_id = #{studentId} AND course_id = #{courseId}")
    @Results({
        @Result(property = "id", column = "id"),
        @Result(property = "studentId", column = "student_id"),
        @Result(property = "courseId", column = "course_id"),
        @Result(property = "enrollmentDate", column = "enrollment_date"),
        @Result(property = "status", column = "status")
    })
    Enrollment findByStudentAndCourse(@Param("studentId") Long studentId, @Param("courseId") Long courseId);

    @Insert("INSERT INTO enrollment (student_id, course_id, status) VALUES (#{studentId}, #{courseId}, #{status})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Enrollment enrollment);

    @Update("UPDATE enrollment SET status = #{status} WHERE id = #{id}")
    int update(Enrollment enrollment);

    @Delete("DELETE FROM enrollment WHERE id = #{id}")
    int deleteById(Long id);

    @Delete("DELETE FROM enrollment WHERE student_id = #{studentId} AND course_id = #{courseId}")
    int deleteByStudentAndCourse(@Param("studentId") Long studentId, @Param("courseId") Long courseId);
}